package club.kingyin.kyblog.controller;

import club.kingyin.kyblog.core.IP;
import club.kingyin.kyblog.core.business.dao.admin.SitePro;
import club.kingyin.kyblog.core.business.dao.admin.Token;
import club.kingyin.kyblog.core.business.dto.ResultUtil;
import club.kingyin.kyblog.core.business.service.admin.SiteProService;
import club.kingyin.kyblog.core.business.service.cloud.QiniuService;
import club.kingyin.kyblog.core.business.vo.admin.ConfigVo;
import club.kingyin.kyblog.core.business.vo.admin.PersonalVo;
import club.kingyin.kyblog.core.business.vo.admin.SiteProVo;
import club.kingyin.kyblog.core.extend.IPSetPostProcessor;
import club.kingyin.kyblog.core.utils.log.LogConsole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ：kingyin
 * @date ：创建于 2021/8/19 10:53 下午
 * @description ：
 * @modified By：
 * @version: 1.0.0
 */
@IP
@RestController
public class AdminSiteController {

    @Autowired
    private SiteProService siteProService;

    @Autowired
    private QiniuService qiniuService;

    // 上传照片的token只能使用一次
//    @Cache(module = "七牛云", operator = "九命可", expire = 1000 * 60 * 60)
    @LogConsole(module = "获得上传Token", operator = "九命可", sources = IPSetPostProcessor.class)
    @GetMapping("/update/token")
    public ResultUtil token() {
        Token token = qiniuService.getUploadToken();
        if (token == null) {
            return ResultUtil.fail("无法获得token");
        }
        return ResultUtil.success(token);
    }

    @LogConsole(module = "获取站点详细信息", operator = "九命可", sources = IPSetPostProcessor.class)
    @RequestMapping("/site/info")
    public ResultUtil siteInfo() {
        SitePro adminSiteInfo = siteProService.getSiteInfo();
        return ResultUtil.success(adminSiteInfo);
    }

    @LogConsole(module = "站点信息更新", operator = "九命可", sources = IPSetPostProcessor.class)
    @PostMapping("/site/update")
    public ResultUtil siteUpdate(@RequestBody SiteProVo site) {
        if (siteProService.updateSite(site)) {
            return ResultUtil.success("更新成功");
        }
        return ResultUtil.fail("更新失败");
    }

    @LogConsole(module = "Config数据更新", operator = "九命可", sources = IPSetPostProcessor.class)
    @PostMapping("/conf/update")
    public ResultUtil confUpdate(@RequestBody ConfigVo config) {
        if (siteProService.updateConf(config.getKey(), config.getValue())) {
            return ResultUtil.success("更新成功");
        }
        return ResultUtil.fail("更新失败");
    }

    @LogConsole(module = "个性化数据", operator = "九命可", sources = IPSetPostProcessor.class)
    @RequestMapping("/site/personal")
    public ResultUtil personalInfo() {
        PersonalVo personal = siteProService.getPersonal();
        if (personal != null) {
            return ResultUtil.success(personal);
        }
        return ResultUtil.fail("加载异常");
    }

}
